package com.zyd.controller.user;

import com.google.common.collect.Maps;
import com.zyd.constants.ErrorDef;
import com.zyd.exception.BusinessException;
import com.zyd.io.model.UserEntity;
import com.zyd.io.vo.page.MultiQueryPageVO;
import com.zyd.io.vo.res.ResBody;
import com.zyd.io.vo.user.LoginRequestVO;
import com.zyd.io.vo.user.ModifyPasswdVO;
import com.zyd.io.page.PageBean;
import com.zyd.io.vo.user.UserVO;
import com.zyd.service.user.UserService;
import com.zyd.util.MD5Utils;
import com.zyd.util.ToolUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserContoller {

    private final static Logger LOGGER = LoggerFactory.getLogger(UserContoller.class);

    @Autowired
    private UserService service;

    @PostMapping(value = "/addUser", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody addUser(@RequestBody UserVO userVO) {
        try {
            UserEntity user = new UserEntity();
            user.setCity(userVO.getCity());
            //昵称
            user.setNickname(userVO.getNickname());
            //用户名称
            user.setUsername(userVO.getUsername());
            //手机号
            user.setMobile(userVO.getPhone());
            user.setUsertype(userVO.getRoleid());

            service.insertUser(user);
            return new ResBody(ErrorDef.SUCCESS, "新增用户成功");
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "新增用户失败");
        }
    }

    @PostMapping(value = "/updateUser",consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody updateUser(@RequestBody UserVO user) {
        try {
            UserEntity model = new UserEntity();
            model.setCity(user.getCity());
            model.setPwd(user.getPassword());
            model.setNickname(user.getNickname());
            model.setUsername(user.getUsername());
            //手机号
            model.setMobile(user.getPhone());
            model.setUsertype(user.getRoleid());
            model.setId(user.getId());

            service.updateUser(model);
            return new ResBody(ErrorDef.SUCCESS, "修改用户成功");
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "修改用户失败");
        }
    }

    @PostMapping(value = "/deleteUser",consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody deleteUser(@RequestBody Map<String, Object> map) {
        try {
            service.deleteUser((String) map.get("userid"));
            return new ResBody(ErrorDef.SUCCESS, "删除用户成功");
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "删除用户失败");
        }
    }

    @PostMapping(value = "/userList",consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody findUseList(@RequestBody MultiQueryPageVO request) {
        try {
            PageBean<UserEntity> userList = service.findUserList(request);
            List<UserVO> ret = new ArrayList<>();
            for (UserEntity user : userList.getItems()) {
                UserVO userVO = new UserVO();
                userVO.setCity(user.getCity());
                userVO.setId(user.getId());
                userVO.setPhone(user.getMobile());
                userVO.setNickname(user.getNickname());
                userVO.setUsername(user.getUsername());
                userVO.setRoleid(user.getUsertype());
                ret.add(userVO);
            }

            PageBean<UserVO> page = new PageBean<>(userList.getCurrentPage(), userList.getPageSize(), userList.getTotalNum());
            page.setItems(ret);

            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "查询用户列表成功");
            resBody.setData(page);
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "查询用户列表失败");
        }
    }

    @PostMapping(value = "/findUser", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody findUser(@RequestBody Map<String, Object> map) {
        try {
            UserEntity user = service.findUser((String) map.get("userid"));
            UserVO userVO = new UserVO();
            userVO.setCity(user.getCity());
            userVO.setId(user.getId());
            userVO.setPhone(user.getMobile());
            userVO.setNickname(user.getNickname());
            userVO.setRoleid(user.getUsertype());
            userVO.setUsername(user.getUsername());
            userVO.setPassword(user.getPwd());

            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "查询用户成功");
            resBody.setData(userVO);
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "查询用户失败");
        }
    }

    @PostMapping(value = "/login")
    public ResBody login(@RequestBody LoginRequestVO requestVO, HttpServletRequest request) {
        try {
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "登录成功");
            Map<String, Object> ret = service.login(requestVO, request.getSession());
            resBody.setData(ret);
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "登录失败");
        }
    }
    @PostMapping(value = "/modifyPasswd")
    public ResBody modifyPasswd(@RequestBody ModifyPasswdVO requestVO) {
        try {
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "修改密码成功");
            service.motifyPasswd(requestVO);
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "修改密码失败");
        }
    }

    /**
     * 重置密码
     * @param param
     * @return
     */
    @PostMapping(value = "/resetPassword")
    public ResBody resetPassword(@RequestBody Map<String,String> param) {
        try {
            String id = param.get("id");
            service.resetPassword(id);
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "重置密码成功");
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "重置密码失败");
        }
    }
}
